package service;

/**
 * @author Lars og Christian
 */

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.concurrent.TimeUnit;

import model.Behandling;
import model.DelBehandling;
import model.DrageringBehandling;
import model.Mellemvare;
import model.PakkeklarBehandling;
import model.ProduktType;
import model.Statistik;
import model.ToerretidBehandling;

import org.junit.Before;
import org.junit.Test;

public class ServiceTest {
	private Service service = Service.getInstance();
	private ArrayList<DelBehandling> list = new ArrayList<DelBehandling>();
	private ArrayList<DelBehandling> list2 = new ArrayList<DelBehandling>();
	private ArrayList<DelBehandling> list3 = new ArrayList<DelBehandling>();
	private ArrayList<DelBehandling> list4 = new ArrayList<DelBehandling>();
	
	private ProduktType chokolade = new ProduktType("M&Ms", "Chokolade lincer");
	private ProduktType skumbanan = new ProduktType("Skumbanan", "Skum");
	private Mellemvare m1 = new Mellemvare(215463, 25000, chokolade);
	private Mellemvare m2 = new Mellemvare(253125, 12500, skumbanan);
	private Behandling b1, b2, b3, b4;
	
	@Before
	public void setUp() throws Exception {	
		try{
			list.add(new ToerretidBehandling(1, 2, 3));
			list.add(new DrageringBehandling(1));
			list.add(new ToerretidBehandling(1, 2, 3));
			list.add(new PakkeklarBehandling(20));
			
			list2.add(new ToerretidBehandling(1, 2, 3));
			list2.add(new PakkeklarBehandling(20));
			
			list3.add(new PakkeklarBehandling(20));
			list3.add(new ToerretidBehandling(1,2,3));
			
			list4.add(new ToerretidBehandling(1, 2, 3));
			list4.add(new DrageringBehandling(1));
		}
		catch(Exception ex){
			ex.getStackTrace();
		}
	}
	
	@Test
	public void testCreateBehandling(){
		//Test1
		try{
		b1 = service.createBehandling("Test", new GregorianCalendar(), list, m1);
		b2 = service.createBehandling("TestTo", new GregorianCalendar(), list2, m2);
		}
		catch (Exception ex) {
			ex.getStackTrace();
		}
		
		assertEquals(Behandling.class, service.getBehandlinger().get(0).getClass());
		assertEquals(Behandling.class, service.getBehandlinger().get(1).getClass());
	}
	
	
	@Test (expected = Exception.class)
	public void fejlCreateBehandling() throws Exception {
		b3 = service.createBehandling("FejlTest", new GregorianCalendar(), list3, m1);
		b4 = service.createBehandling("FejlTestTo", new GregorianCalendar(), list4, m2);
	}

	@Test
	public void testNaesteBehandling() {
		ProduktType p = Service.getInstance().createProduktType("TestType", "Test");
		Mellemvare m = Service.getInstance().createMellemvare(10, p);
		ArrayList<DelBehandling> delbehandlinger = new ArrayList<DelBehandling>();
		try {
			//Test 1
			delbehandlinger.add(new ToerretidBehandling(TimeUnit.MINUTES.toMillis(1), TimeUnit.MINUTES.toMillis(2), TimeUnit.MINUTES.toMillis(3)));
			delbehandlinger.add(new DrageringBehandling(TimeUnit.MINUTES.toMillis(10)));
			delbehandlinger.add(new ToerretidBehandling(TimeUnit.MINUTES.toMillis(1), TimeUnit.MINUTES.toMillis(2), TimeUnit.MINUTES.toMillis(3)));
			delbehandlinger.add(new PakkeklarBehandling(TimeUnit.MINUTES.toMillis(10)));
			
			Behandling b = Service.getInstance().createBehandling("Test1", GregorianCalendar.getInstance(), delbehandlinger, m);
			
			assertEquals(ToerretidBehandling.class, b.getDelbehandlinger().get(0).getClass());
			
			Service.getInstance().naesteBehandling(b);
			
			assertEquals(DrageringBehandling.class, b.getDelbehandlinger().get(0).getClass());
			
			//Test 2
			delbehandlinger.clear();
			delbehandlinger.add(new ToerretidBehandling(TimeUnit.MINUTES.toMillis(1), TimeUnit.MINUTES.toMillis(2), TimeUnit.MINUTES.toMillis(3)));
			delbehandlinger.add(new PakkeklarBehandling(TimeUnit.MINUTES.toMillis(10)));
			
			b = Service.getInstance().createBehandling("Test2", GregorianCalendar.getInstance(), delbehandlinger, m);
			
			assertEquals(ToerretidBehandling.class, b.getDelbehandlinger().get(0).getClass());
			
			Service.getInstance().naesteBehandling(b);
			
			assertEquals(PakkeklarBehandling.class, b.getDelbehandlinger().get(0).getClass());
			
			//Test 3
			delbehandlinger.clear();
			delbehandlinger.add(new ToerretidBehandling(TimeUnit.MINUTES.toMillis(1), TimeUnit.MINUTES.toMillis(2), TimeUnit.MINUTES.toMillis(3)));
			delbehandlinger.add(new PakkeklarBehandling(TimeUnit.MINUTES.toMillis(10)));
			
			b = Service.getInstance().createBehandling("Test3", GregorianCalendar.getInstance(), delbehandlinger, m);
			
			Service.getInstance().naesteBehandling(b);
			
			assertEquals(PakkeklarBehandling.class, b.getDelbehandlinger().get(0).getClass());
			
			Service.getInstance().naesteBehandling(b);
			
			assertTrue(b.getDelbehandlinger().size() == 0);
		}
		catch(Exception ex) {
			ex.printStackTrace();
			fail();
		}
	}
	
	@Test
	public void testToerreTid() {
		try {
			ToerretidBehandling t = new ToerretidBehandling(TimeUnit.MINUTES.toMillis(10), TimeUnit.MINUTES.toMillis(20), TimeUnit.MINUTES.toMillis(30));

			assertEquals(ToerretidBehandling.class, t.getClass());
		}
		catch(Exception ex) {
			ex.printStackTrace();
			fail();
		}
	}
	
	@Test (expected = Exception.class)
	public void fejlToerreTid() throws Exception {
		ToerretidBehandling t = new ToerretidBehandling(TimeUnit.MINUTES.toMillis(20), TimeUnit.MINUTES.toMillis(10), TimeUnit.MINUTES.toMillis(30));
		ToerretidBehandling t2 = new ToerretidBehandling(TimeUnit.MINUTES.toMillis(10), TimeUnit.MINUTES.toMillis(30), TimeUnit.MINUTES.toMillis(20));
	}
	
	@Test
	public void testGenererStatistik() {
		Statistik.getInstance().setAntalBehandlinger(10);
		Statistik.getInstance().setAntalForGamleBehandlinger(1);
		
		assertEquals(10.0, Statistik.getInstance().getForGamleProcent(), 0.1);
		
		Statistik.getInstance().setAntalBehandlinger(10);
		Statistik.getInstance().setAntalForGamleBehandlinger(9);
		
		assertEquals(90.0, Statistik.getInstance().getForGamleProcent(), 0.1);
	}
}